﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VipCo.Data;
using VipCo.Entities;

public partial class Admin_BinhLuan_CNComment : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {

            if (Request.QueryString["id"] != null)
            {
                H_CatalogId.Value = Request.QueryString["id"].ToString();
                BindData(Convert.ToInt32(H_CatalogId.Value));
            }
        }
    }

    protected void BindData(int IdComment)
    {
        BinhLuanTin entComment = DataRepository.BinhLuanTinProvider.GetById(IdComment);
        H_TinTuc.Value = entComment.IdTinTuc.ToString();
        chkTrangThai.Checked = entComment.TrangThai;
        txtTieuDe.Text = entComment.TieuDe;
        txtTen.Text = entComment.HoTen;
        txtMail.Text = entComment.Mail;
        txtNoiDung.Text = entComment.NoiDung;
    }

    protected void cmdLuu_Click(object sender, ImageClickEventArgs e)
    {
        bool updateStatus = false;
        try
        {
            BinhLuanTin entComment = new BinhLuanTin();
            entComment.Id = Convert.ToInt32(H_CatalogId.Value);
            entComment.IdTinTuc = Convert.ToInt32(H_TinTuc.Value);
            entComment.HoTen = Common.StandardString(txtTen.Text);
            entComment.TieuDe = Common.StandardString(txtTieuDe.Text);
            entComment.Mail = Common.StandardString(txtMail.Text);
            entComment.NoiDung = Server.HtmlEncode(txtNoiDung.Text);
            entComment.TrangThai = chkTrangThai.Checked;
            entComment.NgayCapNhat = Common.GetServerDateTime();
            if (entComment.Id == 0)
            {
                updateStatus = DataRepository.BinhLuanTinProvider.Insert(entComment);
            }
            else
            {
                entComment.EntityState = EntityState.Changed;
                updateStatus = DataRepository.BinhLuanTinProvider.Update(entComment);
            }
            if (!updateStatus)
                WebMessage.Show("Cập nhật Comment thất bại!");
            else
            {
                WebMessage.Show("Cập nhật Comment thành công!");
                H_CatalogId.Value = entComment.Id.ToString();
            }
        }
        catch (Exception ex)
        {
            Common.WriteLog("CNComment.aspx.cs", "cmdLuu_Click", ex.ToString());
            WebMessage.Show("Cập nhật Comment thất bại!");
        }
    }

    protected void cmdNhapTiep_Click(object sender, ImageClickEventArgs e)
    {
        H_CatalogId.Value = "0";

        txtTen.Text = "";
        txtTieuDe.Text = "";
        txtMail.Text = "";
        chkTrangThai.Checked = false;
        txtNoiDung.Text = "";
    }
}